package cn.marinda.resturants.controller;

import cn.marinda.resturants.entity.Activity;
import cn.marinda.resturants.global.Pages;
import cn.marinda.resturants.service.ActivityService;
import cn.marinda.resturants.utils.ApiResult;
import cn.marinda.resturants.utils.PagesResult;
import cn.marinda.resturants.utils.SpringBeanUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;

/** 活动控制器
 * @data 2021/11/29
 * @author Marinda
 *
 */
@RestController
@RequestMapping("/activity")
public class ActivityController {

    @Resource
    private ActivityService activityService;


    /** 返回活动列表
     * @version 1.0
     * @author Marinda
     * @date 2021/11/29
     *
     */

    @RequestMapping("/queryList")
    public ApiResult queryList(){
        List<Activity> list = activityService.queryList();
        return new ApiResult(ApiResult.ON_SUCCESS,ApiResult.SUCCESS_MESSAGE,list);
    }

    /** 通过uid来查询活动信息
     *
     * @param uid 活动uid
     * @return 结果
     */
    @RequestMapping("/index")
    public ApiResult selectActivityByUid(String uid){
        Activity activity = activityService.selectByUid(uid);
        return new ApiResult(ApiResult.ON_SUCCESS,ApiResult.SUCCESS_MESSAGE, Arrays.asList(activity));
    }


    /** 通过模糊查询内容来获取某个活动
     *
     * @param content 内容
     * @return 活动
     */
    @RequestMapping("/content")
    
    public ApiResult selectContentLike(String content){
        Activity activity = activityService.selectContentLike(content);
        return new ApiResult(ApiResult.ON_SUCCESS,ApiResult.SUCCESS_MESSAGE,activity);
    }

    /** 通过模糊查询标题来获取某个活动
     *
     * @param title 标题
     * @return 活动
     */
    @RequestMapping("/title")
    
    public ApiResult selectTitleLike(String title) {

        Activity activity = activityService.selectTitleLike(title);
        return new ApiResult(ApiResult.ON_SUCCESS, ApiResult.SUCCESS_MESSAGE, activity);
    }

    /** 通过模糊查询链接来找到这个活动
     *
     * @param href 链接
     * @return 活动
     */
    @RequestMapping("/href")
    
    public ApiResult selectHrefLike(String href){
        Activity activity = activityService.selectHrefLike(href);
        return new ApiResult(ApiResult.ON_SUCCESS,ApiResult.SUCCESS_MESSAGE,activity);
    }

    /** 列表分页
     *
     * @date 2021/12/1
     * @return 结果
     */
    @RequestMapping("/page")
    public PagesResult selectPages(@RequestParam("page") int page, @RequestParam("limit") int limit)
    {
        IPage<Activity> pages = activityService.selectActivityPages(new Pages<Activity>(page,limit));
        return new PagesResult(0,pages.getRecords(),(int)pages.getTotal(),pages.getSize());
    }


    /** 增加一个活动
     *
     * @param activity 活动
     * @return 活动
     */
    @RequestMapping("/add")
    public ApiResult insertActivity(Activity activity){
        return  activityService.addActivity(activity) ? new ApiResult(ApiResult.ON_SUCCESS,ApiResult.SUCCESS_MESSAGE,null): new ApiResult(ApiResult.ON_FAIL,ApiResult.FAIL_MESSAGE,null);
    }

    /**
     * 删除活动
     *
     * @param uid uid
     * @return {@link ApiResult}
     */
    @RequestMapping("/delete")
    public ApiResult deleteActivity(String uid){
        return  activityService.deleteActivity(uid) ? new ApiResult(ApiResult.ON_SUCCESS,ApiResult.SUCCESS_MESSAGE,null): new ApiResult(ApiResult.ON_FAIL,ApiResult.FAIL_MESSAGE,null);
    }

    /** 一个悲观锁查询
     * 要加事务处理回滚操作。
     * @date 2021/11/18
     * @author Marinda
     */
    @Transactional(isolation = Isolation.READ_COMMITTED)
    @RequestMapping("/lockSelect")
    public ApiResult selectPessimisticLock(String uid){
        Activity activity = activityService.selectPessimisticLock(uid);
        return new ApiResult(ApiResult.ON_SUCCESS,ApiResult.SUCCESS_MESSAGE,activity);
    }

    /** 一个乐观锁修改数据
     *
     * @param activity 活动
     * @return 活动
     */
    @RequestMapping("/update")
    public ApiResult updateOptimisticLock(Activity activity){

        return activityService.updateOptimisticLock(activity) ? new ApiResult(ApiResult.ON_SUCCESS, ApiResult.SUCCESS_MESSAGE,null):   new ApiResult(ApiResult.ON_FAIL, ApiResult.FAIL_MESSAGE,null);
    }



}
